﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Mvc;
using NetCoreCMS.IServices;
using NetCoreCMS.Models;

namespace NetCoreCMS.Web.Areas.WebApi.Controllers
{
    public class User_billsController : UserController
    {
        public IBillsService _bills { get; set; }
        [Authorize]
        [HttpGet]
        public IActionResult Get(int page = 1, int page_size = 20)
        {
            //检查用户是否登录
            Users userModel = GetUserInfo();
            if (userModel == null)
            {
                return Ok(new { status = 0, msg = "用户尚未登录或已超时！" });
            }
            page = page > 0 ? page : 1;
            string strwhere = "where user_id=" + userModel.id;
            var list = _bills.GetList(page, page_size, strwhere, "add_time desc,id desc");
            int totalcount = _bills.RecordCount(strwhere);
            int pageCount = totalcount / page_size;
            if (pageCount < 1)
            {
                pageCount = 1;
            }
            if (totalcount % page_size > 0)
            {
                pageCount += 1;
            }
            if (pageCount <= 1)
            {
                pageCount = 1;
            }
            List<DTO.UserBills> dtos = new List<DTO.UserBills>();
            foreach (var item in dtos)
            {
                DTO.UserBills dto = new DTO.UserBills();
                dto.user_name = userModel.user_name;
                dto.status_name = "奖金";
                dto.id = item.id;
                dto.user_id = item.user_id;
                dto.point = item.point;
                dto.remark = item.remark;
                dto.status = item.status;
                dto.add_time = item.add_time;
                dtos.Add(dto);
            }
            return Ok(new { status = 1, list = dtos, page = page, page_count = pageCount, total_count = totalcount });
        }
    }
}